/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketManager.controller;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import ticketManager.ejb.HistoryDAO;
import ticketManager.ejb.ProblemDAO;
import ticketManager.model.ManagerUser;
import ticketManager.model.Problem;
import ticketManager.model.ProblemStates;

/**
 *
 * @author Administrator
 */
@Named
@RequestScoped
public class ChangeProblemStateController {

    @EJB
    ProblemDAO pDAO;
    @EJB
    HistoryDAO hDAO;
    EntityManager em;
    private Problem p;
    private ManagerUser m;
    private ProblemStates select_state;
    private String title;
    private String description;
    private ArrayList<ProblemStates> statess = new ArrayList<>();
    FacesContext facesContext = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
//
    String id = request.getParameter("problemId");

    public String getTitle() {
        if (request.getParameter("problemId") != null) {
            int p_temp_id = Integer.parseInt(id);
            Problem p_temp = pDAO.getProblemById(new Long((long) p_temp_id));
            title = p_temp.getTitle();
            facesContext.getExternalContext().getSessionMap().put("pId", p_temp_id);
            facesContext.getExternalContext().getSessionMap().put("title", title);

        } else {
            title = (String) facesContext.getExternalContext().getSessionMap().get("title");
        }

        return title;
    }

    public String getDescription() {
        if (request.getParameter("problemId") != null) {
            int p_temp_id = Integer.parseInt(id);
            Problem p_temp = pDAO.getProblemById(new Long((long) p_temp_id));
            description = p_temp.getDescription();
            facesContext.getExternalContext().getSessionMap().put("description", description);
        } else {
            description = (String) facesContext.getExternalContext().getSessionMap().get("description");
        }

        return description;
    }

//    public Problem getP() {
////        System.out.println("getP");
////
//////        System.out.println("nuber" + p_temp_id);
//        
//        return p_temp;
//    }
    public ArrayList<ProblemStates> getStatess() {

        statess.add(ProblemStates.NewlyCreated);
        statess.add(ProblemStates.InWork);
        statess.add(ProblemStates.Testing);
        statess.add(ProblemStates.Solved);
        statess.add(ProblemStates.Rejected);
        statess.add(ProblemStates.TransferRequest);
        return statess;
    }

    public ProblemStates getSelect_state() {

        return select_state;
    }

    public void setSelect_state(ProblemStates select_state) {
        this.select_state = select_state;
    }

    public ProblemDAO getpDAO() {
        return pDAO;
    }

    public void setpDAO(ProblemDAO pDAO) {
        this.pDAO = pDAO;
    }

    public String doChangeState() {

        int p_temp_id = (int) facesContext.getExternalContext().getSessionMap().get("pId");
        Problem p_temp2 = pDAO.getProblemById(new Long((long) p_temp_id));
        m = (ManagerUser) facesContext.getExternalContext().getSessionMap().get("user");
        Problem p_final = pDAO.changeProblemState(p_temp2, select_state);
        hDAO.changeProblemStateEvent(m, p_final);
        return "success";
    }

    public String changeStateToRejected(Long pId) {
        System.err.println("change state executed");
        Problem p_temp2 = pDAO.getProblemById(pId);
        m = (ManagerUser) facesContext.getExternalContext().getSessionMap().get("user");
        ProblemStates ps = ProblemStates.Rejected;
        Problem p_final = pDAO.changeProblemState(p_temp2, ps);
        hDAO.changeProblemStateEvent(m, p_final);
        return "The problem has been rejected.";
        
    }
}
